package tcpserver.handler.in;

import frame.laxcen.BaseFrame;
import frame.laxcen.response.Response_0001_12;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import sun.plugin2.message.HeartbeatMessage;

import java.util.function.Consumer;

public class InterceptorOfHeartBeats extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        BaseFrame baseFrame = BaseFrame.class.cast(msg);

        if(baseFrame.isResponse(Response_0001_12.class)) {

            final Consumer<Response_0001_12> heartbeatConsumer =
                    (frame) -> ctx.channel().writeAndFlush(frame.reflectToRequest());

            baseFrame.castToResponse(Response_0001_12.class).ifPresent(heartbeatConsumer);
        }

        else ctx.fireChannelRead(msg);
    }
}
